package cn.com.pc.content.jpa;

import cn.com.pc.content.domain.Site;

import javax.persistence.AttributeConverter;
import java.util.HashMap;
import java.util.Map;

/**
 * 站点转为 string
 */
public class SiteConverter implements AttributeConverter<Site, String> {
    final Map<String, Site> map;

    public SiteConverter() {
        map = new HashMap<>();
        for (Site s : Site.values()) {
            map.put(s.name(), s);
        }
    }

    @Override
    public String convertToDatabaseColumn(Site attribute) {
        return attribute == null ? null : attribute.name();
    }

    @Override
    public Site convertToEntityAttribute(String dbData) {
        return map.getOrDefault(dbData, Site.Unknown);
    }
}
